<template>
    <div class="bg">
        <div class="login_box">
            <div class="avator_box">
                <img src="../../assets/home_page2.png" alt="">
            </div>

            <div class="form_box">
                <el-form :model="loginForm" ref="loginForm" :rules="loginRules" label-width="80px">
                    <el-form-item prop="account" label="用户名：">
                        <el-input prefix-icon="el-icon-user" v-model='loginForm.account'></el-input>
                    </el-form-item>
                    <el-form-item prop="pwd" label="密 码 ：">
                        <el-input prefix-icon="el-icon-lock" type="password" v-model='loginForm.pwd'></el-input>
                    </el-form-item>
                    <el-form-item prop="captcha" label="验证码：" class="captcah-item">
                        <el-input prefix-icon="el-icon-finished" v-model='loginForm.captcha'></el-input>
                        <img :src="imgcode" alt="" @click="getImgCode">
                    </el-form-item>
                    
                    <el-form-item class='btn-box'>
                        <el-button type="primary" @click='login'>登录</el-button>
                        <el-button @click='clearForm'>清空</el-button>
                    </el-form-item>
                </el-form>
            </div>
            
        </div>
    </div>
</template>

<script>
import {login,getCaptcha} from '@/api/login/login'
import {setCookies, getCookie} from '@/lib/utils'
import Setting from '@/Setting'

export default {
    
    data(){
        return {
           loginForm:{ 
               account:'',
               pwd:'',
               captcha:'',
            },
            loginRules:{
                username: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
                ],
                captcha:[
                    { required: true, message: '请输入验证码', trigger: 'blur' },
                ]
            },
            imgcode:'',
        }  
    },
    methods:{
        login(){
           
           login(this.loginForm).then(res=>{
                let flag = setCookies(Setting.tokenName,res.data.token);
                //this.$message.success("登录成功")
                this.$router.push({name:'main'});
            }).catch(err=>{
                this.$message.error("登录失败："+err)
            });

            this.$refs.loginForm.validate((res,field)=>{
                if(!res){return false}
                
            })
        },
        clearForm(){
            this.$refs.loginForm.resetFields()
        },
        
        getImgCode(){
            this.imgcode = Setting.apiBaseURL+'login/getCaptcha?' + Date.parse(new Date());
        }
    },
    mounted() {
        this.getImgCode()
    },
}
</script>

<style lang="less" scoped>
    .bg{
        background-color: #2b4b6b;
        position:absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
    .login_box{
        width: 500px;
        height: 350px;
        border-radius: 10px;
        background: #FFF;
        position:absolute;
        left: 50%;
        top:50%;
        transform: translate(-50%,-50%);

        .avator_box{
            width: 130px;
            height: 130px;
            border-radius: 50%;
            border: 1px solid #EEE;
            box-shadow: 0 0 10px #ddd;
            padding:10px;
            background: #FFF;
            position: absolute;
            left: 50%;
            transform: translate(-50%,-50%);
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background: #EEE;
            }
        }
        .btn-box{
            display: flex;
            justify-content: flex-end;
        }
        .form_box{
            position: absolute;
            bottom: 0;
            width: 100%;
            padding: 10px;
            box-sizing: border-box;
            .captcah-item{
                position: relative;
                img{
                    position: absolute;
                    top: 1px;
                    right: 2px;
                }
            }
        }
    }
</style>
